package com.xunk.scene.controller;

import com.xunk.common.core.annotation.EnumCheck;
import com.xunk.common.core.utils.StringUtils;
import com.xunk.common.core.web.domain.AjaxResult;
import com.xunk.scene.constant.SceneConstant;
import com.xunk.scene.domain.DeviceNotifyEntity;
import com.xunk.scene.enums.FlagOneZeroEnum;
import com.xunk.scene.service.SceneService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @ProjectName: HIM
 * @Package: com.xunk.scene.controller
 * @ClassName: SceneController
 * @Author: luanzhiwei
 * @Description: 场景控制层
 * @Date: 2021/5/6 18:57
 * @Version: 1.0
 */
@RestController
@Validated
@Slf4j
public class SceneController {

    @Autowired
    private SceneService sceneService;


    /**
     * 添加场景,添加场景模板，isModel字段0模板，1场景
     *
     * @param params
     * @return
     */
    @PostMapping("/addScene")
    public AjaxResult addScene(@RequestBody Map<String, Object> params) throws Exception {
        return sceneService.addScene(params);
    }

    /**
     * 编辑场景
     *
     * @return
     */
    @PostMapping("/editScene")
    public AjaxResult editScene(@RequestBody Map<String, Object> params) throws Exception {
        return sceneService.editScene(params);
    }

    /**
     * 查询家庭id下全部场景概览
     *
     * @param homeId
     * @param appFlag  1是APP，0 是pad
     * @param homePage 首页标识 1是0否，该字段传1查首页,并且不区分手动和自动列表。该字段传0或不传查自定义,区分手动和自动列表
     * @return
     */
    @GetMapping(value = "/getScenes")
    public AjaxResult getScene(@RequestParam(name = "homeId") Long homeId,
                               @RequestParam(name = "appFlag", required = false) String appFlag,
                               @RequestParam(name = "homePage", required = false) Long homePage) {
        return sceneService.getScenes(homeId, appFlag, homePage);
    }

    /**
     * 根据场景id，查询场景详细内容
     *
     * @param sceneId
     * @return
     */
    @GetMapping(value = "/getSceneDetail")
    public AjaxResult getSceneDetail(@RequestParam(name = "sceneId") Long sceneId, @RequestParam(name = "appFlag", required = false) String appFlag) {
        return sceneService.getSceneDetail(sceneId, appFlag);
    }

    /**
     * 删除场景
     *
     * @param sceneId
     * @param homeId
     * @return
     */
    @GetMapping(value = "/delScene")
    public AjaxResult delScene(@RequestParam("sceneId") long sceneId, @RequestParam(name = "homeId") Long homeId) {
        return sceneService.delScene(sceneId, homeId);
    }

    /**
     * 添加场景到我的家庭
     *
     * @return
     */
    @PostMapping(value = "/addToMyHome")
    public AjaxResult addToMyHome(@RequestBody Map<String, Object> params) throws Exception {
        return sceneService.addToMyHome(params);
    }


    /**
     * 接收设备消息推送
     */
    @GetMapping(value = "/notify")
    public void receiveEquipmentStatus(@RequestParam("deviceSn") String deviceSn, @RequestParam("type") String type, @RequestParam("value") String value) throws Exception {
        log.info("接收设备服务消息推送");
        DeviceNotifyEntity notifyEntity = new DeviceNotifyEntity(deviceSn, type, value);
        boolean match = sceneService.matchCondition(notifyEntity);
        if (!match) {
            return;
        }
        sceneService.execSceneAction(StringUtils.EMPTY, notifyEntity.getRedisValue());
    }

    /**
     * 查询模板场景
     *
     * @return
     */
    @GetMapping(value = "/getModels")
    public AjaxResult getModels() {
        return sceneService.getModels();
    }

    /**
     * 查询场景模板详情
     *
     * @param sceneId
     * @param homeId
     * @return
     */
    @GetMapping(value = "/getModelDetail")
    public AjaxResult getModelDetail(@RequestParam(name = "sceneId") Long sceneId,
                                     @RequestParam(name = "homeId") Long homeId,
                                     @RequestParam(name = "appFlag", required = false) String appFlag) {
        return sceneService.getModelDetail(sceneId, homeId, appFlag);
    }

    /**
     * 开启/关闭场景
     *
     * @param homeId
     * @param sceneId
     * @param activeFlag (0关闭，1启动)
     * @return
     */
    @GetMapping(value = "/changeActiveFlag")
    public AjaxResult closeScene(@RequestParam(name = "homeId") Long homeId,
                                 @RequestParam(name = "sceneId") Long sceneId,
                                 @RequestParam(name = "activeFlag") @EnumCheck(enableClass = FlagOneZeroEnum.class, message = "activeFlag为错误的枚举值") String activeFlag) {
        if (sceneService.changeStatus(homeId, sceneId, activeFlag)) {
            return AjaxResult.success();
        }
        return AjaxResult.error(StringUtils.equals(SceneConstant.SCENE_ACTIVE_Y, activeFlag) ? "场景启动失败" : "场景关闭失败");
    }

    /**
     * 根据家庭id和场景id查询全部执行记录
     *
     * @param homeId
     * @param sceneId   非必填
     * @param pageIndex 当前页数，默认一页10条数据
     * @return
     */
    @GetMapping(value = "/getExecLog")
    public AjaxResult getExecLog(@RequestParam(name = "homeId") Long homeId,
                                 @RequestParam(name = "sceneId", required = false) Long sceneId,
                                 @RequestParam(name = "pageIndex") Long pageIndex) {
        return sceneService.getExecLog(homeId, sceneId, pageIndex);
    }

    /**
     * 根据记录id删除记录
     *
     * @param homeId
     * @return
     */
    @GetMapping(value = "/delExecLog")
    public AjaxResult delExecLog(@RequestParam("homeId") Long homeId) {
        return sceneService.delExecLog(homeId);
    }

    /**
     * 上传场景模板头像/用户头像
     *
     * @param name       模板图像显示名称
     * @param selected   选中图标、用户自定义头像
     * @param unselected 未选中图标
     * @param sceneId
     * @return
     */
    @PostMapping(value = "/uploadSceneIcon")
    public AjaxResult uploadSceneIcon(@RequestParam(name = "name", required = false) String name,
                                      @RequestParam(name = "unselected") MultipartFile unselected,
                                      @RequestParam(name = "selected", required = false) MultipartFile selected,
                                      @RequestParam(name = "sceneId", required = false) Long sceneId) {
        return sceneService.uploadSceneIcon(name, unselected, selected, sceneId);
    }

    /**
     * 添加模板头像为我的头像
     *
     * @param id
     * @param sceneId
     * @return
     */
    @GetMapping(value = "/addSceneIconToMyScene")
    public AjaxResult addSceneIconToMyScene(@RequestParam(name = "id") Long id, @RequestParam(name = "sceneId") Long sceneId) {
        return sceneService.addSceneIconToMyScene(id, sceneId);
    }

    /**
     * 查询全部场景头像
     *
     * @return
     */
    @GetMapping(value = "/getSceneIcons")
    public AjaxResult getSceneIcon(@RequestParam(name = "appFlag", required = false) String appFlag) {
        return sceneService.getSceneIcon(appFlag);
    }

    /**
     * 添加场景到首页，移除首页场景
     *
     * @param homeId
     * @param sceneId
     * @param homePage
     * @return
     */
    @GetMapping(value = "/changeHomePage")
    public AjaxResult changeHomePage(@RequestParam(name = "homeId") Long homeId,
                                     @RequestParam(name = "sceneId") Long sceneId,
                                     @RequestParam(name = "homePage") String homePage) throws Exception {
        return sceneService.changeHomePage(homeId, sceneId, homePage);
    }


}
